package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.LogisticsInfoAdaper;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.HomeBean;
import com.zhongjian.technologycal.mall.bean.LoginBean;
import com.zhongjian.technologycal.mall.bean.LogisticsBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.ListViewForScrollView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 物流信息的实现
 */

public class LogisticsInformationActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.logistis_iv_product_status)
    TextView logistis_iv_product_status;
    @BindView(R.id.logistis_iv_product_no)
    TextView logistis_iv_product_no;
    @BindView(R.id.logistis_iv_product_phone)
    TextView logistis_iv_product_phone;
    @BindView(R.id.logistis_iv_product_listview)
    ListViewForScrollView logistis_iv_product_listview;

    @BindView(R.id.logistis_refreshLayout)
    RefreshLayout logistis_refreshLayout;
    private List<LogisticsBean> listItems = new LinkedList<>();
    private String orderNo;
    private LogisticsInfoAdaper logisticsInfoAdaper;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    logisticsInfoAdaper.notifyDataSetChanged();
                    break;
            }

        }
    };


    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("物流跟踪");
        orderNo = getIntent().getStringExtra("orderNo");
        initViewRequestLogisticsInformation(orderNo);

        logisticsInfoAdaper = new LogisticsInfoAdaper(getBaseContext(), listItems);
        logistis_iv_product_listview.setAdapter(logisticsInfoAdaper);
        logistis_refreshLayout.setEnableLoadmore(false);
        logistis_refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                logistis_refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initViewRequestLogisticsInformation(orderNo);
                        logistis_refreshLayout.finishRefresh();
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_logistics_information;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    /**
     * 请求物流信息接口数据
     */
    private void initViewRequestLogisticsInformation(String orderNo) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestLogisticsInformation(UserUtils.getUserToken(), orderNo,
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(LogisticsInformationActivity.this);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUitl.showShort(e.getMessage());
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            LogisticsBean logisticsBean = gson.fromJson(responseBody.string(), LogisticsBean.class);
                            if (logisticsBean.getError_code() == 0) {
                                if (logisticsBean.getData().getState().equals("2")) {
                                    logistis_iv_product_status.setText("运输中");
                                } else if (logisticsBean.getData().getState().equals("3")) {
                                    logistis_iv_product_status.setText("已签收");
                                } else if (logisticsBean.getData().getState().equals("4")) {
                                    logistis_iv_product_status.setText("问题件");
                                }
                                logistis_iv_product_no.setText("承运方：" + logisticsBean.getData().getShipperCode());
                                logistis_iv_product_phone.setText("物流单号：" + logisticsBean.getData().getLogisticCode());
                                List<LogisticsBean> forList = new ArrayList<>();
                                for (int i = 0; i < logisticsBean.getData().getTraces().size(); i++) {
                                    forList.add(logisticsBean);
                                }
                                setData(forList);
                            } else if (logisticsBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(logisticsBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void setData(List<LogisticsBean> foreshowList) {
        if (listItems.size() > 0) {
            listItems.clear();
        }
        for (LogisticsBean vo : foreshowList) {
            listItems.add(vo);
        }
        Collections.reverse(listItems);
        Log.v("hh", listItems.get(0).getData().getTraces().get(0).getAcceptStation());
        handler.sendEmptyMessage(0);

    }


}
